package com.tw.management.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tw.management.entity.User;
import com.tw.management.service.UserService;
import com.tw.management.utlis.PasswordAndSaltHelper;

/**
 * home page.
 */
@Controller
@RequestMapping("/")
public class IndexController {

	private static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Resource(name = "userService")
	private UserService service;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info(" Index Page. ");
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate);
		return "index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(User userLogin) {
		logger.info(" login. ");
		logger.info("userLogin:" + userLogin.toString());
		Assert.hasText(userLogin.getEmail(), "email can not be null");
		Assert.hasText(userLogin.getPassword(), "password can not be null");
		User userFind = service.findUserByEmail(userLogin.getEmail());
		if (userFind == null) {
			return "redirect:/login";
		} else {
			userLogin.setSalt(userFind.getSalt());
			PasswordAndSaltHelper helper = new PasswordAndSaltHelper();
			String passwordLogin = helper.decryptPassword(userLogin);
			if (!passwordLogin.equals(userFind.getPassword())) {
				return "redirect:/login";
			} else {
				if ("0".equals(userFind.getUserType())) {
					return "redirect:/admin/";
				}
				return "redirect:/user/";
			}
		}
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register() {
		return "register";
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(User userRegister) {
		logger.info(" register. ");
		logger.info("userLogin:" + userRegister.toString());
		Assert.hasText(userRegister.getEmail(), "email can not be null");
		Assert.hasText(userRegister.getPassword(), "password can not be null");
		Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		if (!pattern.matcher(userRegister.getEmail()).matches()) {
			return "redirect:/register";
		}
		userRegister.setUserType("1");
		userRegister.setCreateDate(new Date());
		PasswordAndSaltHelper helper = new PasswordAndSaltHelper();
		helper.encryptPassword(userRegister);
		userRegister = service.saveUser(userRegister);
		return "redirect:/";
	}

}
